package com.game.modular.game.battle.process.battleStage;

import com.game.modular.game.battle.info.roundInfo.PvpBattleRoundInfoServiceImpl;
import com.game.modular.game.battle.panel.attribute.domain.EffectStatus;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.game.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.game.modular.game.battle.process.skillEffectHandle.skillEffectSelect.SkillEffectTriggerHandle;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  回合前准备
 */
@Slf4j
@Service
public class PreparationStageOfRoundService {
    @Resource
    private PvpBattleRoundInfoServiceImpl pvpBattleRoundInfoService;
    @Resource
    private PvpSkillEffectServiceImpl skillEffectService;
    @Resource
    private PlayerHeroAttributeServiceImpl playerHeroAttributeService;


    /**
     * 回合前准备阶段
     *
     * @param battleId      战斗id
     * @param round         回合数
     * @param orderOfAction 行动顺序
     * @param flush         行动武将列表
     */
    public List<PlayerHero> apply(
            Long battleId,
            Integer round,
            Integer orderOfAction,
            List<PlayerHero> flush
    ) {
        pvpBattleRoundInfoService.initBattleRoundInfo(battleId, round, orderOfAction);
        log.info("=======所有武将进入回合准备阶段");
        for (PlayerHero playerHero : flush) {
            PlayerHeroAttribute playerHeroAttribute = playerHeroAttributeService.get(battleId, playerHero.getId());
            playerHeroAttribute.setPhase(20);
            playerHeroAttributeService.saveCache(playerHeroAttribute);
        }

        // 尝试触发被动战法
        log.info("------刷新被动战法");
        List<PvpSkillEffect> skillEffectList2 =
                skillEffectService.allTriggeringList(battleId, 2, round);
        skillEffectList2 = SkillEffectTriggerHandle.applyNewSkillEffect(skillEffectList2);

        // 尝试触发指挥战法
        log.info("------刷新指挥战法");
        List<PvpSkillEffect> skillEffectList3 =
                skillEffectService.allTriggeringList(battleId, 3, round);
        skillEffectList3 = SkillEffectTriggerHandle.applyNewSkillEffect(skillEffectList3);

        List<PvpSkillEffect> list = new ArrayList<>();
        list.addAll(skillEffectList2);
        list.addAll(skillEffectList3);


        return obtainActHeroList(battleId, flush);
    }

    /**
     * 获取行动武将
     * 先手状态 > 先手值高
     */
    public List<PlayerHero> obtainActHeroList(
            @NotNull(message = "战斗id不能为空") Long battleId,
            @NotNull(message = "可行动的武将不能为空") List<PlayerHero> actionList
    ) {
        Map<String, PlayerHeroAttribute> map = playerHeroAttributeService.mapCache(battleId);
        Map<Long, PlayerHero> heroMap = actionList.stream()
                .collect(Collectors.toMap(PlayerHero::getId, hero -> hero));
        return actionList.stream().map(item -> map.get(item.getId().toString())).sorted((o1, o2) -> {
            EffectStatus speed1 = o1.getStatus().get(36L);
            EffectStatus speed2 = o2.getStatus().get(36L);

            if (speed1 != null && speed2 == null) {
                return -1; // o1 优先
            } else if (speed1 == null && speed2 != null) {
                return 1; // o2 优先
            } else { // 同时拥有状态或者没有状态
                return o1.getSpeed().compareTo(o2.getSpeed());
            }
        }).map(item -> heroMap.get(item.getId())).collect(Collectors.toList());
    }
}
